package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PatrolArea;
import com.ruoyi.system.domain.bo.PatrolAreaBo;
import com.ruoyi.system.domain.vo.PatrolAreaVo;
import com.ruoyi.system.mapper.PatrolAreaMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.IPatrolAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 巡区管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-01
 */
@RequiredArgsConstructor
@Service
public class PatrolAreaServiceImpl implements IPatrolAreaService {

    private final PatrolAreaMapper baseMapper;

    private final SysDeptMapper deptMapper;

    /**
     * 查询巡区管理
     */
    @Override
    public PatrolAreaVo queryById(Long areaId){
        return baseMapper.selectVoById(areaId);
    }

    /**
     * 查询巡区管理列表
     */

    @Override
    public TableDataInfo<PatrolAreaVo> queryPageList(PatrolAreaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PatrolArea> lqw = buildQueryWrapper(bo);
        Page<PatrolAreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        IPage<PatrolAreaVo> convert = result.convert(k ->
        {
            Long deptId = k.getDeptId();
            SysDept sysDept = deptMapper.selectVoById(deptId);
            k.setDeptName(sysDept.getDeptName());
            return k;
        });

        return TableDataInfo.build(convert);
    }

    /**
     * 查询巡区管理列表
     */
    @Override
    public List<PatrolAreaVo> queryList(PatrolAreaBo bo) {
        LambdaQueryWrapper<PatrolArea> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PatrolArea> buildQueryWrapper(PatrolAreaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PatrolArea> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAreaName()), PatrolArea::getAreaName, bo.getAreaName());
        lqw.eq(bo.getAreaType() != null, PatrolArea::getAreaType, bo.getAreaType());
        lqw.eq(bo.getHasFence() != null, PatrolArea::getHasFence, bo.getHasFence());
        lqw.eq(bo.getDeptId() != null, PatrolArea::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PatrolArea::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增巡区管理
     */
    @Override
    public Boolean insertByBo(PatrolAreaBo bo) {
        PatrolArea add = BeanUtil.toBean(bo, PatrolArea.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAreaId(add.getAreaId());
        }
        return flag;
    }

    /**
     * 修改巡区管理
     */
    @Override
    public Boolean updateByBo(PatrolAreaBo bo) {
        PatrolArea update = BeanUtil.toBean(bo, PatrolArea.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PatrolArea entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除巡区管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
